package com.jiuli.liping.controller;

import com.jiuli.liping.vo.Classification;
import com.jiuli.liping.vo.Commodity;
import com.jiuli.liping.vo.CommodityFather;
import com.jiuli.liping.vo.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.jiuli.liping.service.CommodityService;

import java.util.List;

/**
 * Author zyl
 * Date  2020-05-18
 */
@CrossOrigin
@RestController
@RequestMapping(value = "/commodity")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    @RequestMapping(value = {"/list", ""}, method = RequestMethod.GET)
    public Object list() {
        List<Commodity> commoditys = commodityService.findAllList();
        return commoditys;
    }

    @RequestMapping(value = {"/get"}, method = RequestMethod.GET)
    public Object get(@RequestParam String id) {
        Commodity commodity = commodityService.get(id);
        return commodity;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public String insert(@RequestBody Commodity commodity) {
        if (commodityService.insert(commodity) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    @RequestMapping(value = "/insertBatch", method = RequestMethod.POST)
    public String insertBatch(@RequestBody List<Commodity> commoditys) {
        if (commodityService.insertBatch(commoditys) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(@RequestBody Commodity commodity) {
        if (commodityService.update(commodity) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(@RequestBody Commodity commodity) {
        if (commodityService.delete(commodity) > 0) {
            return "success";
        } else {
            return "failed";
        }
    }

    /**
     * 获取所有商品信息入库用
     * @return
     */
    @RequestMapping(value = "/selectAllCommodity", method = RequestMethod.POST)
    public Message selectAllCommodity() {
        Message mess = new Message();
        List<Classification> list = commodityService.selectAllCommodity();
        if(list == null){
            mess.setFlag("2");
            mess.setMessage("数据获取失败");
        }else{
            mess.setFlag("1");
            mess.setMessage("数据获取成功");
            mess.setClassificationList(list);
        }
        return mess;
    }

    /**
     * 获取所有商品信息分类加载
     * @return
     */
    @RequestMapping(value = "/selectAllCommodityList", method = RequestMethod.POST)
    public Message selectAllCommodityList() {
        Message mess = new Message();
        List<Classification> list = commodityService.selectAllCommodityList();
        if(list == null){
            mess.setFlag("2");
            mess.setMessage("数据获取失败");
        }else{
            mess.setFlag("1");
            mess.setMessage("数据获取成功");
            mess.setClassificationList(list);
        }
        return mess;
    }

    /**
     * 获取所有商品信息首页加载
     * @return
     */
    @RequestMapping(value = "/selectAllCommoditySy", method = RequestMethod.POST)
    public Message selectAllCommoditySy() {
        Message mess = new Message();
        List<Classification> list = commodityService.selectAllCommoditySy();
        if(list == null){
            mess.setFlag("2");
            mess.setMessage("数据获取失败");
        }else{
            mess.setFlag("1");
            mess.setMessage("数据获取成功");
            mess.setClassificationList(list);
        }
        return mess;
    }

    /**
     * 获取商品详细信息
     * @return
     */
    @RequestMapping(value = "/selectCommodityByFatherNumber", method = RequestMethod.POST)
    public Message selectCommodityByFatherNumber(@RequestBody Commodity commodity) {
        Message mess = new Message();
        mess = commodityService.selectCommodityByFatherNumber(commodity);
        return mess;
    }

    /**
     * 新增商品
     * @return
     */
    @RequestMapping(value = "/insertCommodity", method = RequestMethod.POST)
    public Message insertCommodity(@RequestBody Commodity commodity) {
        Message me = new Message();
        if(commodity.getCommodityName() == null){
            me.setFlag("0");
            me.setMessage("数据信息不准确");
            return me;
        }
        if(commodity.getPhotographList() == null){
            me.setFlag("0");
            me.setMessage("数据信息不准确");
            return me;
        }
        /*if(commodity.getPropertyList() == null){
            me.setFlag("0");
            me.setMessage("数据信息不准确");
            return me;
        }*/
        if(commodity.getProviderList() == null){
            me.setFlag("0");
            me.setMessage("数据信息不准确");
            return me;
        }
        int flag = commodityService.insertCommodity(commodity);
        if(flag == 1){
            me.setFlag("1");
            me.setMessage("新增商品成功");
            return me;
        }else if(flag == 2){
            me.setFlag("2");
            me.setMessage("该商品名称已存在");
            return me;
        }
        else {
            me.setFlag("0");
            me.setMessage("新增商品失败");
            return me;
        }
    }

    /**
     * 商品搜索
     */
    @RequestMapping(value = "/getProductByName", method = RequestMethod.POST)
    public List<Classification> getProductByName(@RequestBody Commodity commodity) {
        Message mess = new Message();
        List<Classification> list = commodityService.getProductByName(commodity);
        /*if(list == null){
            mess.setFlag("2");
            mess.setMessage("数据获取失败");
        }else{
            mess.setFlag("1");
            mess.setMessage("数据获取成功");
            mess.setClassificationList(list);
        }*/
        return list;
    }
}
